home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / TransSkel / Demos / C Demos / Button / Dialog3.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-11  |  5.4 KB  |  206 lines  |  [TEXT/KAHL]

  1. /*
  2.  * Dialog 3
  3.  *
  4.  * This dialog consists of:
  5.  * - A Dismiss pushbutton for dismissing the dialog.
  6.  * - Three pushbuttons, Button 1, Button 2, and Button 3, any
  7.  * of which may be selected as the default button.  It is also
  8.  * possible for none of the buttons to be selected.
  9.  * - Four radio buttons used to select which of Button[1-3]
  10.  * is the default, or that none of them is.
  11.  * - Three checkboxes for activating or deactivating Button[1-3].
  12.  * - A couple of static text items for titling the radio button
  13.  * and check box sets.
  14.  * - A user item that's used for drawing the outline around the
  15.  * default button.
  16.  *
  17.  * This dialog demonstrates:
  18.  * - How to associate an outlining function with an arbitrary button.
  19.  * - How to change which button is the default, including how to move
  20.  * the outlining function from one button to another.
  21.  * - How to make sure the outline is redrawn properly when the hiliting
  22.  * state of the outlined button changes.
  23.  *
  24.  * The routines that associate the outline item with buttons or dissociate
  25.  * the item from buttons are written in a more modular fashion than for
  26.  * Dialog 2.  They're also written more generally, since they must handle
  27.  * the case where the default button can be set explicitly to "none".
  28.  */
  29.  
  30. # include    "TransSkel.h"
  31.  
  32. # include    "Button.h"
  33.  
  34.  
  35. typedef enum
  36. {
  37.     iPushDismiss = 1,
  38.     iPushButton1,
  39.     iPushButton2,
  40.     iPushButton3,
  41.     iRadioStaticText,
  42.     iRadioButton1,
  43.     iRadioButton2,
  44.     iRadioButton3,
  45.     iRadioNone,
  46.     iCheckStaticText,
  47.     iCheckButton1,
  48.     iCheckButton2,
  49.     iCheckButton3,
  50.     iOutline
  51. };
  52.  
  53.  
  54. static short        defaultButton = 0;
  55.  
  56.  
  57. /*
  58.  * Draw heavy outline around default dialog button.  This function is
  59.  * associated with the user item when there is a default button, and
  60.  * is called by ModalDialog() when the outline needs redrawing.
  61.  */
  62.  
  63. static pascal void
  64. OutlineButton (DialogPtr dlog, short item)
  65. {
  66.     SkelDrawButtonOutline (SkelGetDlogCtl (dlog, defaultButton));
  67. }
  68.  
  69.  
  70. /*
  71.  * Install outlining procedure to associate outline user item with
  72.  * the given button and draw the outline item immediately.
  73.  */
  74.  
  75. static void
  76. InstallOutliner (DialogPtr dlog, short item)
  77. {
  78. Rect    r;
  79.  
  80.     SkelGetDlogRect (dlog, item, &r);            /* get button rect */
  81.     InsetRect (&r, -4, -4);                        /* expand it */
  82.     SkelSetDlogRect (dlog, iOutline, &r);        /* use for outline item */
  83.     SkelSetDlogProc (dlog, iOutline, OutlineButton);
  84.     SkelDrawButtonOutline (SkelGetDlogCtl (dlog, defaultButton));
  85. }
  86.  
  87.  
  88. /*
  89.  * Remove outlining procedure from current default and erase the
  90.  * current outline.
  91.  */
  92.  
  93. static void
  94. RemoveOutliner (DialogPtr dlog)
  95. {
  96.     SkelSetDlogProc (dlog, iOutline, nil);
  97.     SkelEraseButtonOutline (SkelGetDlogCtl (dlog, defaultButton));
  98. }
  99.  
  100.  
  101. /*
  102.  * Set up button given by item number as default button:
  103.  * - Remove the outliner for the current default, if there is one.
  104.  * - Install an outliner for the new default, if there is one.
  105.  */
  106.  
  107. static void
  108. SetDefaultButton (DialogPtr dlog, short item)
  109. {
  110.     if (defaultButton != 0)
  111.         RemoveOutliner (dlog);
  112.     defaultButton = item;
  113.     if (defaultButton != 0)
  114.         InstallOutliner (dlog, defaultButton);
  115. }
  116.  
  117.  
  118. void
  119. DoDialog3 (void)
  120. {
  121. ModalFilterProcPtr    filter;
  122. DialogPtr    dlog;
  123. GrafPtr    savePort;
  124. short    item;
  125. short    value;
  126. short    hilite;
  127. short    loop;
  128.  
  129.     dlog = GetNewDialog (dlog3Res, nil, (WindowPtr) -1L);
  130.     if (dlog == (DialogPtr) nil)
  131.     {
  132.         SysBeep (1);
  133.         return;
  134.     }
  135.  
  136.     SkelPositionWindow (dlog, skelPositionOnMainDevice, horizRatio, vertRatio);
  137.  
  138.     GetPort (&savePort);
  139.     SetPort (dlog);
  140.  
  141.     SetDefaultButton (dlog, iPushButton1);
  142.     SkelSetDlogCtlValue (dlog, iCheckButton1, 1);
  143.     SkelSetDlogCtlValue (dlog, iCheckButton2, 1);
  144.     SkelSetDlogCtlValue (dlog, iCheckButton3, 1);
  145.     SkelSetDlogRadioButtonSet (dlog, iRadioButton1, iRadioNone, iRadioButton1);
  146.  
  147.     ShowWindow (dlog);
  148.  
  149.     loop = 1;
  150.     while (loop)
  151.     {
  152.         /*
  153.          * Get the standard filter and specify the default button so it will
  154.          * map return/enter.  If the default is zero, mapping is turned off.
  155.          *
  156.          * Note: the mapping causes the default button to flash when return/enter
  157.          * are typed, but the dialog isn't dismissed until Dismiss is clicked,
  158.          * which is unlike normal dialog operation.
  159.          */
  160.         filter = SkelDlogFilter (nil, false);
  161.         SkelDlogDefaultItem (defaultButton);    /* turns off if zero */
  162.         ModalDialog (filter, &item);
  163.         SkelRmveDlogFilter ();
  164.  
  165.         switch (item)
  166.         {
  167.         case iPushDismiss:
  168.             loop = 0;
  169.             break;
  170.         case iPushButton1:        /* ignore hits in these items */
  171.         case iPushButton2:
  172.         case iPushButton3:
  173.             break;
  174.         case iRadioButton1:
  175.         case iRadioButton2:
  176.         case iRadioButton3:
  177.             SkelSetDlogRadioButtonSet (dlog, iRadioButton1, iRadioNone, item);
  178.             /* remap item number from radio button range into pushbutton range */
  179.             item += iPushButton1 - iRadioButton1;
  180.             SetDefaultButton (dlog, item);
  181.             break;
  182.         case iRadioNone:
  183.             SkelSetDlogRadioButtonSet (dlog, iRadioButton1, iRadioNone, item);
  184.             SetDefaultButton (dlog, 0);    /* no default button */
  185.             break;
  186.         case iCheckButton1:
  187.         case iCheckButton2:
  188.         case iCheckButton3:
  189.             value = SkelToggleDlogCtlValue (dlog, item);
  190.             hilite = (value ? normalHilite : dimHilite);
  191.             /* remap item number from checkbox range into pushbutton range */
  192.             item += iPushButton1 - iCheckButton1;
  193.             /*
  194.              * Set button's hiliting state to make it active or inactive.
  195.              * If the new state is different than previous and the button
  196.              * is the default, redraw the outline.
  197.              */
  198.             if (SkelSetDlogCtlHilite (dlog, item, hilite) && item == defaultButton)
  199.                 SkelDrawButtonOutline (SkelGetDlogCtl (dlog, item));
  200.             break;
  201.         }
  202.     }
  203.     DisposeDialog (dlog);
  204.     SetPort (savePort);
  205. }
  206.